package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.entity.po.Class;
import com.srzk.youthleagueback.mapper.ClassMapper;
import com.srzk.youthleagueback.results.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@Service
public class ClassService {
    @Autowired
    ClassMapper classMapper;

    public Result<Class> addClass(Class cla) {
        int result = classMapper.insert(cla);
        if (result == 1) {
            return Result.success(cla);
        } else {
            return Result.error(ADD_FAIL);
        }
    }

    public Result<List<Class>> getList() {
        List<Class> classes = classMapper.selectAll();
        if (classes == null || classes.isEmpty()) {
            return Result.error(NOT_EXIST);
        } else {
            return Result.success(classes);
        }
    }


    public Result<Class> getByName(String name, String grade, String dname) {
        Class cla = classMapper.selectByPrimaryKey(name, grade, dname);
        if (cla == null) {
            return Result.error(NOT_EXIST);
        } else {
            return Result.success(cla);
        }
    }

    public Result<Class> update(Class cla) {
        int result = classMapper.insert(cla);
        if (result == 1) {
            return Result.success(cla);
        } else {
            return Result.error(UPDATE_FAIL);
        }
    }

}
